from DrissionPage import ChromiumPage

from DrissionPage.common import Keys

import csv

import time

from openpyxl.worksheet import page

with open('earphoneTaoBao.csv', mode='w', encoding='UTF-8-sig', newline='') as f:

    csv_writer = csv.DictWriter(f, fieldnames=[
        '标题',
        '价格',
        '付款人数',
        '详情页'
    ])

    csv_writer.writeheader()

    dp = ChromiumPage()

    dp.get(
        #'https://uland.taobao.com/sem/tbsearch?clk1=8c715d010a2859976effa2096d6abf2e&keyword=%E7%B2%AE%E6%B2%B9%E7%B1%B3%E9%9D%A2&localImgKey=&msclkid=133b7806bea515ba80df1fd55dc951f8&page=1&q=%E7%B2%AE%E6%B2%B9%E7%B1%B3%E9%9D%A2&refpid=mm_2898300158_3078300397_115665800437&tab=all&upsId=8c715d010a2859976effa2096d6abf2e')
        'https://www.taobao.com/?bc_fl_src=tbsite_4Bhzid61&msclkid=86b6c9d866e511e00bb3ae01514c1a36')
    input_box = dp.ele('css:#q')  # 获取 id 为 "q" 的输入框

    if input_box:
        input_box.clear()  # 清空输入框
        input_box.input('耳机')  # 输入 "耳机"
    else:
        print("未找到 id 为 'q' 的输入框")

    dp.ele('css:#q').input(Keys.ENTER)


    #dp.wait.load_start()  # 等待页面开始加载
    time.sleep(2)  # 额外等待2秒，确保页面稳定

    handles = dp.driver  # 获取所有窗口句柄
    print(handles)  # 查看窗口列表

    # for i in range(1, 10):
    dp.scroll.to_bottom()
    time.sleep(5)

    # li = dp.ele('xpath://*[@id="item_id_892131726737"]/div/div[1]/div[2]/div/span').text.replace('\n', ' ')
    # print(li)

    # dp.wait.ele_displayed('css:.content--CUnfXXxv')
    #
    # lis = dp.eles('css:.content--CUnfXXxv')
    #
    # if lis:
    #     print(lis)
    # for container in lis:
    #     sub_items = container.eles('span')  # 获取每个容器内的 span
    #     for sub in sub_items:
    #         print(sub.text)

    # content = dp.eles('css:.content--CUnfXXxv')  # 获取外层容器
    # if content:
    #     sub_elements = content.eles('css:div')  # 获取内部所有 div
    #     for sub in sub_elements:
    #         print(sub.text)
    # print(len(content))

    all_divs = dp.eles('css:div')
    for div in all_divs:
        print(div.attr('class'), ":", div.text)

    exit()
    # 循环一下提取数据
    for li in lis:
        title = li.ele('css:.title--qJ7Xg_90 span').text.replace('\n', ' ')
        try:
            price1 = li.ele('css:.priceInt--yqqZMJ5a')
            price2 = li.ele('css:.priceFloat--XpixvyQ1')
            # .text.replace('\n', ' ')
            price1_text = price1.text.strip() if price1 else "0"  # 如果找不到整数部分，默认是 "0"
            price2_text = price2.text.strip() if price2 else "00"  # 如果找不到小数部分，默认是 "00"
            price = f"{price1_text}{price2_text}"
        except:
            price = '预售'
        try:
            commit = li.ele('css:.realSales--XZJiepmt').text.replace('\n', ' ')
        except:
            commit = '新品上市'
        link = li.ele('css:.doubleCardWrapperAdapt--mEcC7olq ')  # 查找 <a> 标签

        href = link.attr('href')

        dit = {
            '标题': title,
            '价格': price,
            '付款人数': commit,
            '详情页': href
        }
        csv_writer.writerow(dit)
        # print(dit)
            # try:
            #     button = li.ele('css:.next-btn next-medium next-btn-normal next-pagination-item next-next')
            #     button.click()
            # except:
            #     continue

        #下一页
        # lis[0].input(Keys.RIGHT)